import * as module from "@/tools/util";
import * as Request from '@/api/liubike';
import store from "@/tools/factory"
import { config, isDev } from '@/api/config';

import wx  from '@/tools/wx'
import commonJs from '@/views/commonJs/common'
let that
let loginInfo = null;

/**
 * 转化:   this.data -> this._data
 * ev.detail -> ev.target
 */

export default {
    mixins: [commonJs],
    data() {
        return {
            title: "加载中...", // 状态
            list: [], // 数据列表
            type: "", // 数据类型
            pageConfig: {
                offset: 0,
                limit: 20
            },
            footerVisbale: false,
            loading: true // 显示等待框
        }
    },
    mounted() {
        that = this;
        this.getInfo();
    },
    methods: {
        onLoad(options) {
            that = this;
            this.getInfo();
        },
        onShow() {

        },
        onShareAppMessage: function() {
            return {
                title: '小遛共享',
                path: '/index',
            }
        },
        goPay(e) {
            const status = e.currentTarget.dataset.status;
            if (status == 3) {
                if (!config.switchParkPay) {
                    wx.redirectTo({
                        url: `/parkPay`
                    });
                    return false
                }
                store.set("pageSource", "ARREARS_PAY")
                wx.redirectTo({
                    url: `/payOrder`
                })
            }
        },
        getInfo(callback) { // 获取优惠券列表
            let APPDATA = that._data;
            wx.showLoading({
                title: '加载中...',
            })
            Request.getMytrip(
                APPDATA.pageConfig
            ).then(res => {
                wx.hideLoading();
                console.log(res, 'resss')
                if (res.length == 0) {
                    that.setData({
                        footerVisbale: true
                    })
                    return;
                }
                if (res.length < 20 && APPDATA.pageConfig.offset == 0) {
                    that.setData({
                        footerVisbale: true
                    })
                }
                res.forEach((item, index) => {
                    item.statusStr = that.typeToStr(item.status);
                    if (item.starttime && item.endtime) {
                        let end = +new Date(item.endtime);
                        let start = +new Date(item.starttime);
                        item.rideTime = that.formatSeconds(((end - start) / 60000) * 60);
                    }
                    let date = new Date(item.starttime);
                    item.starttime = that.fomatDate(date, "yyyy.MM.dd hh:mm");
                    item.distance = (item.distance / 1000).toFixed(2);
                    if (item.status == 3) {
                        item.moneyDisplay = item.money.toFixed(2);
                    } else {
                        item.moneyDisplay = (item.paymoney + item.giveMoney + item.returnMoney).toFixed(2);
                    }
                });
                console.log('sjsjsjsjsjj')
                that.setData({
                    list: that._data.list.concat(res)
                })

            }).catch(err => {
                wx.hideLoading();
                wx.showToast({
                    title: '小遛需要缓缓',
                    icon: 'none',
                    duration: 2000
                })

            }).finally(res=> {
                callback && callback()
            });
        },
        formatSeconds(value) {
            if (!value) {
                return ""
            }
            var secondTime = parseInt(value); // 秒
            var minuteTime = 0; // 分
            var hourTime = 0; // 小时
            if (secondTime > 60) {
                //如果秒数大于60，将秒数转换成整数
                //获取分钟，除以60取整数，得到整数分钟
                minuteTime = parseInt(secondTime / 60);
                //获取秒数，秒数取佘，得到整数秒数
                secondTime = parseInt(secondTime % 60);
                //如果分钟大于60，将分钟转换成小时
                if (minuteTime > 60) {
                    //获取小时，获取分钟除以60，得到整数小时
                    hourTime = parseInt(minuteTime / 60);
                    //获取小时后取佘的分，获取分钟除以60取佘的分
                    minuteTime = parseInt(minuteTime % 60);
                }
            }
            var result = "" + parseInt(secondTime) + "秒";

            if (minuteTime > 0) {
                result = "" + parseInt(minuteTime) + "分" + result;
            }
            if (hourTime > 0) {
                result = "" + parseInt(hourTime) + "小时" + result;
            }
            return result;
        },
        fomatDate(date, fmt) {
            if (/(y+)/.test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
            }
            let o = {
                'M+': date.getMonth() + 1,
                'd+': date.getDate(),
                'h+': date.getHours(),
                'm+': date.getMinutes(),
                's+': date.getSeconds()
            };
            for (let k in o) {
                if (new RegExp(`(${k})`).test(fmt)) {
                    let str = o[k] + '';
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : that.padLeftZero(str));
                }
            }
            return fmt;
        },
        typeToStr(type) {
            switch (type + "") {
                case "3":
                    return "待支付";
                default:
                    return "已完成";
            }
        },
        padLeftZero(str) {
            return ('00' + str).substr(str.length);
        },
        onReachBottom(callback) {
            if (that._data.footerVisbale == true) {
                return false;
            }
            that.setData({
                'pageConfig.offset': that._data.pageConfig.offset + 20
            })
            console.log(callback, 'callback')
            // that.getInfo(function home() {
            //    console.log('这是home页面')
            // })
            that.getInfo(callback)
        }
    }
}
